package com.example.menuvirtualclient.rule;

import java.util.List;

import android.content.Context;

import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.data.ISession;
import com.example.menuvirtualclient.data.SessionFactory;
import com.example.menuvirtualclient.repository.*;

public class CommentRule {

	private ISession _session;
	private final CommentRepository _repository;

	public CommentRule(Context context) {
		_session = SessionFactory.GetSession(context);
		_repository = new CommentRepository(context);
	}
	
	public List<Comment> GetCommentByDetail(int detailId) throws Exception {
		try {
			_session.Open();
			return _repository.getAllbyOrderDetail(detailId);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public Comment Get(int id) throws Exception{
		try {
			_session.Open();
			return _repository.Get(id);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Insert(Comment entity) throws Exception{
		try {
			_session.Open();
			_repository.Insert(entity);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Insert(List<Comment> comments) throws Exception{
		try {
			_session.Open();
			for(Comment entity : comments){
				if(!exist(entity)) {
					_repository.Insert(entity);
				}
				else {
					_repository.Update(entity);
				}
			}
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void Update(Comment entity) throws Exception{
		try {
			_session.Open();
			_repository.Update(entity);			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}	
		
	public void deleteComments() throws Exception{
		try {
			if(!_session.IsOpen())
				_session.Open();
			//_repository.DeleteOrderDetailComments();
			_repository.DeleteDishComments();
			_repository.Delete();
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void deleteOrderDetailCommentsById(int detailId) throws Exception{
		try {
			if(!_session.IsOpen())
				_session.Open();
			_repository.deleteOrderDetailCommentsById(detailId);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void saveOrderDetailComment(int orderdetailid, int commentid) throws Exception {
		try {
			_session.Open();
			_repository.saveOrderDetailComment(orderdetailid, commentid);
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public void saveOrderDetailComment(int orderdetailid, List<Integer> commentids) throws Exception {
		try {
			_session.Open();
			for(int id : commentids){
				_repository.saveOrderDetailComment(orderdetailid, id);
			}			
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
	public boolean exist(Comment entity) throws Exception{
		try {
			_session.Open();
			Comment comment = _repository.Get(entity.getId());
			if(comment != null)
				return true;
			else
				return false;
		} 
		catch (Exception ex) {
			throw ex;
		}
		finally{
			_session.Close();
		}
	}
	
}
